package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Education;
import cn.wolfcode.crm.domain.EmployeeNeeds;
import cn.wolfcode.crm.qo.EmployeeNeedsQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.RecruitPlanQueryObject;
import cn.wolfcode.crm.service.IEmployeeNeedsService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("employeeNeeds")
@Controller
public class EmployeeNeedsController {
    @Autowired
    private IEmployeeNeedsService employeeNeedsService;

    @RequestMapping("view")
    public String list() {
        return "/recruitPlan/employeeNeeds";
    }

    //异步加载数据
    @RequestMapping("query")
    @ResponseBody
    public Object list(@ModelAttribute("qo") EmployeeNeedsQueryObject qo, Model model) {
        //将数据转换成json格式
        PageResult result = employeeNeedsService.query(qo);

        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            employeeNeedsService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("");
        }
        return JSONResult.success("删除成功!");
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(EmployeeNeeds employeeNeeds) {
        try {
            if (employeeNeeds.getId() == null)
                employeeNeedsService.insert(employeeNeeds);
            else
                employeeNeedsService.updateByPrimaryKey(employeeNeeds);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("保存失败!");
        }
        return JSONResult.success("保存成功!");
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<EmployeeNeeds> selectAll() {
        return employeeNeedsService.selectAll();
    }
}
